<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="发布人" prop="publisher">
        <el-input v-model="dataForm.publisher" placeholder="发布人"></el-input>
      </el-form-item>
      <el-form-item label="采购量" prop="amount">
        <el-input v-model="dataForm.amount" placeholder="采购量"></el-input>
      </el-form-item>
      <el-form-item label="品种" prop="varieties">
        <el-input v-model="dataForm.varieties" placeholder="品种"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="date">
        <el-input v-model="dataForm.date" placeholder="时间"></el-input>
      </el-form-item>
      <el-form-item label="货源地点" prop="place">
        <el-input v-model="dataForm.place" placeholder="货源地点"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        publisher: "",
        amount: "",
        varieties: "",
        price: "",
        phone: "",
        date: "",
        place: ""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
         publisher: [{ required: true, message: "发布人不能为空", trigger: "blur" }],
        amount: [{ required: true, message: "采购量不能为空", trigger: "blur" }],
       varieties: [{ required: true, message: "品种不能为空", trigger: "blur" }],
       price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
       date: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ],
        place: [
          { required: true, message: "货源地点不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/huinong/purchaseInformation/delete`),
            method: "post",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name;
              this.dataForm.publisher = data.supply.publisher;
               this.dataForm.amount = data.supply.amount;
              this.dataForm.varieties = data.supply.varieties;
              this.dataForm.price = data.supply.price;
              this.dataForm.phone = data.supply.phone;
              this.dataForm.date = data.supply.date;
              this.dataForm.place=data.supply.place;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/purchaseInformation/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name:this.dataForm.name,
              publisher: this.dataForm.publisher,
              amount: this.dataForm.amount,
              varieties: this.dataForm.varieties,
             price: this.dataForm.price,
              place: this.dataForm.place,
              phone: this.dataForm.phone,
              date: this.dataForm.date,
              place:this.dataForm.place
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
